// CS 362 - Team #18
/* randomGenerator.c
 * This utility helps to supply random generators for Dominion testing.
 */
 #include <stdlib.h>
 #include <stdio.h>
 #include "randomGenerator.h"
 #include "dominion.h"
 #include "interface.h"
 #include "rngs.h"
 #include <time.h>


/*
 makeRandomKingdomCards()
 */
 void makeRandomKingdomCards(struct gameState *state, int kingdom_cards[]){
	/*
	 * Objective: Pick 10 random kingdom cards and removing them from selection if already picked.
	 * According to dominion.h, the following kingdom cards are available (20 total):
	 *	-adventurer, council_room, feast, gardens, mine, remodel, smithy, village
	 *  -baron, great_hall, minion, steward, tribute, ambassador, cutpurse, embargo
	 *  -outpost, salvager, sea_hag, treasure_map
	 */
	 int i = 0;
	 int pickRandom = 0;
	 int pickKingdom = 0;
	 int kingdomCardCount = 0;
	 
	srand(time(0)); // seed a random number via time

	 while(kingdomCardCount < 11){
	 	// grab a random kingdom card
	 	pickRandom = rand() % 21;
	 	//printf("Random #: %d\n", pickRandom);
	 	switch(pickRandom){
	 		case 1: // adventurer
	 			pickKingdom = adventurer;
	 			break;
	 		case 2: // council room
	 			pickKingdom = council_room;
	 			break;
	 		case 3: // feast
	 			pickKingdom = feast;
	 			break;
 			case 4: // gardens
 				pickKingdom = gardens;
 				break;
			case 5: // mine
				pickKingdom = mine;
				break;
			case 6: // remodel
				pickKingdom = remodel;
				break;
			case 7: // smithy
				pickKingdom = smithy;
				break;
			case 8: // village
				pickKingdom = village;
				break;
			case 9: // baron
				pickKingdom = baron;
				break;
			case 10: // great hall
				pickKingdom = great_hall;
				break;
			case 11: // minion
				pickKingdom = minion;
				break;
			case 12: // steward
				pickKingdom = steward;
				break;
			case 13: // tribute
				pickKingdom = tribute;
				break;
			case 14: // ambassador
				pickKingdom = ambassador;
				break;
			case 15: // cutpurse
				pickKingdom = cutpurse;
				break;
			case 16: // embargo
				pickKingdom = embargo;
				break;
			case 17: // outpost
				pickKingdom = outpost;
				break;
			case 18: // salvager
				pickKingdom = salvager;
				break;
			case 19: // sea hag
				pickKingdom = sea_hag;
				break;
			case 20: // treasure map
				pickKingdom = treasure_map;
				break;
	 		default:
	 			break;
	 	}

	 	// check that card has not been selected
	 	if(kingdomCardCount == 0){
	 		kingdom_cards[0] = pickKingdom;
	 		kingdomCardCount++;
	 		//printf("KINGDOM CARD #1: %d. Kingdom Card Count: %d\n", kingdom_cards[0], kingdomCardCount);
	 	}else{
	 		//printf("Current Kingdom Card Count: %d\n", kingdomCardCount);
		 	for(i = 0; i < kingdomCardCount; i++){
		 		if(kingdom_cards[i] == pickKingdom){
		 			//printf("ALREADY PICKED %d: %d. Kingdom Card Count: %d\n", i, pickKingdom, kingdomCardCount);
		 			kingdomCardCount--; // decrement to ensure we pick another card
		 			break;
		 		}else if(i == kingdomCardCount - 1){
		 			kingdom_cards[i] = pickKingdom;
		 			//printf("KINGDOM CARD #%d: %d. Kingdom Card Count: %d\n", i+1, kingdom_cards[i], kingdomCardCount);
		 		}
		 	}
		 	kingdomCardCount++;
		}
	 	//printf("Kingdom Card Count: %d\n", kingdomCardCount);
	 }
	 //return kingdom_cards[];
}

int makeRandomNumPlayers(struct gameState *state, int numPlayers){
	int numPlayerSeed = 5;
	srand(time(0)); // seed a random number via time
	while(numPlayers < 2 || numPlayers > 4){
		numPlayers = rand() % numPlayerSeed;
	}
	return numPlayers;
}

/*
 makeRandomSeed()
 */
int makeRandomSeed(int seed){
	srand(time(0));
	seed = rand();
	return seed;
}

// Objective of this function is to help decide what cards to focus play on
void makeRandomPlayPriorities(int ten_cards[], FILE * file){
	int i = 0, j = 0;
	int actionCardCount = 0;
	int buyCardCount = 0;
	int pickKingdom = 0;
	int pickCard = 0; // used on the buy card list
	int pickRandom = 0;
	int count = 0;
	int allActionPossibilities[19];
	int allBuyPossibilities[24]; // excluding curse & copper for reasonable strategy purposes
	int fifteen_cards[15];
	char cardName[MAX_STRING_LENGTH];
	int randSeed = 0;

	srand(time(0));

	// Generate actionPriority, which is limited to kingdom cards only
	while(actionCardCount < 21){
	 	// extra randomization to ensure priority doesn't match kingdom card selection
	 	randSeed = rand() % 10;
	 	pickRandom = randSeed*rand() % 21;
	 	//printf("Random #: %d\n", pickRandom);
	 	switch(pickRandom){
	 		case 1: // adventurer
	 			pickKingdom = adventurer;
	 			break;
	 		case 2: // council room
	 			pickKingdom = council_room;
	 			break;
	 		case 3: // feast
	 			pickKingdom = feast;
	 			break;
 			case 4: // gardens
 				pickKingdom = gardens;
 				break;
			case 5: // mine
				pickKingdom = mine;
				break;
			case 6: // remodel
				pickKingdom = remodel;
				break;
			case 7: // smithy
				pickKingdom = smithy;
				break;
			case 8: // village
				pickKingdom = village;
				break;
			case 9: // baron
				pickKingdom = baron;
				break;
			case 10: // great hall
				pickKingdom = great_hall;
				break;
			case 11: // minion
				pickKingdom = minion;
				break;
			case 12: // steward
				pickKingdom = steward;
				break;
			case 13: // tribute
				pickKingdom = tribute;
				break;
			case 14: // ambassador
				pickKingdom = ambassador;
				break;
			case 15: // cutpurse
				pickKingdom = cutpurse;
				break;
			case 16: // embargo
				pickKingdom = embargo;
				break;
			case 17: // outpost
				pickKingdom = outpost;
				break;
			case 18: // salvager
				pickKingdom = salvager;
				break;
			case 19: // sea hag
				pickKingdom = sea_hag;
				break;
			case 20: // treasure map
				pickKingdom = treasure_map;
				break;
	 		default:
	 			break;
	 	}

	 	if(actionCardCount == 0){
	 		allActionPossibilities[0] = pickKingdom;
	 		actionCardCount++;
	 	}else{
	 		for(i = 0; i < actionCardCount; i++){
		 		if(allActionPossibilities[i] == pickKingdom){
		 			actionCardCount--; // decrement to ensure we pick another card
		 			break;
		 		}else if(i == actionCardCount - 1){
		 			allActionPossibilities[i] = pickKingdom;
		 		}
		 	}
		 	actionCardCount++;
		}
	 }

	 fprintf(file, "\nAction Card Priority is as follows: \n");
	 for(i = 0; i < actionCardCount - 1; i++){
	 	cardNumToName(allActionPossibilities[i], cardName);
	 	fprintf(file, "%d: %s\n", i+1, cardName);
	 }

	// Build actionPriority based on kingdom cards
	count = 0;
	for(i = 0; i < 20; i++){ // iterate through all possible action cards
		for(j = 0; j < 10; j++){ // iterate through selected kingdom cards
			if(allActionPossibilities[i] == ten_cards[j]){
				actionPriority[count] = allActionPossibilities[i];
				count++;
			}
		}
	}

	fprintf(file, "\nAction Card Priority this game is as follows: \n");
	for(i = 0; i < 10; i++){
		cardNumToName(actionPriority[i], cardName);
		fprintf(file, "%d: %s\n", i+1, cardName);
	}

	// Generate buyPriority, which is focused on kingdom cards, victory cards, and silver/gold treasure card.
	while(buyCardCount < 25){
	 	// extra randomization to ensure priority doesn't match kingdom card selection
	 	randSeed = rand() % 10;
	 	pickRandom = randSeed*rand() % 26;
	 	//printf("Random #: %d\n", pickRandom);
	 	switch(pickRandom){
	 		case 1: // adventurer
	 			pickCard = adventurer;
	 			break;
	 		case 2: // council room
	 			pickCard = council_room;
	 			break;
	 		case 3: // feast
	 			pickCard = feast;
	 			break;
 			case 4: // gardens
 				pickCard = gardens;
 				break;
			case 5: // mine
				pickCard = mine;
				break;
			case 6: // remodel
				pickCard = remodel;
				break;
			case 7: // smithy
				pickCard = smithy;
				break;
			case 8: // village
				pickCard = village;
				break;
			case 9: // baron
				pickCard = baron;
				break;
			case 10: // great hall
				pickCard = great_hall;
				break;
			case 11: // minion
				pickCard = minion;
				break;
			case 12: // steward
				pickCard = steward;
				break;
			case 13: // tribute
				pickCard = tribute;
				break;
			case 14: // ambassador
				pickCard = ambassador;
				break;
			case 15: // cutpurse
				pickCard = cutpurse;
				break;
			case 16: // embargo
				pickCard = embargo;
				break;
			case 17: // outpost
				pickCard = outpost;
				break;
			case 18: // salvager
				pickCard = salvager;
				break;
			case 19: // sea hag
				pickCard = sea_hag;
				break;
			case 20: // treasure map
				pickCard = treasure_map;
				break;
			case 21: // estate
				pickCard = estate;
				break;
			case 22: // duchy
				pickCard = duchy;
				break;
			case 23: // province
				pickCard = province;
				break;
			case 24: // silver
				pickCard = silver;
				break;
			case 25: // gold
				pickCard = gold;
				break;
	 		default:
	 			break;
	 	}

	 	if(buyCardCount == 0){
	 		allBuyPossibilities[0] = pickCard;
	 		buyCardCount++;
	 	}else{
	 		for(i = 0; i < buyCardCount; i++){
		 		if(allBuyPossibilities[i] == pickCard){
		 			buyCardCount--; // decrement to ensure we pick another card
		 			break;
		 		}else if(i == buyCardCount - 1){
		 			allBuyPossibilities[i] = pickCard;
		 		}
		 	}
		 	buyCardCount++;
		}
	 }

	 fprintf(file, "\nBuy Card Priority is as follows: \n");
	 for(i = 0; i < buyCardCount - 1; i++){
	 	cardNumToName(allBuyPossibilities[i], cardName);
	 	fprintf(file, "%d: %s\n", i+1, cardName);
	 }

	 // Create a hybrid of kingdom cards & the 5 extra options
	 for(i = 0; i < 10; i++){
	 	fifteen_cards[i] = ten_cards[i];
	 }
	 // Manually add the 5 missing options
	 fifteen_cards[10] = province;
	 fifteen_cards[11] = silver;
	 fifteen_cards[12] = duchy;
	 fifteen_cards[13] = gold;
	 fifteen_cards[14] = estate;

	 fprintf(file, "\n15 Card Check is as follows: \n");
	 for(i = 0; i < 15; i++){
	 	cardNumToName(fifteen_cards[i], cardName);
	 	fprintf(file, "%d: %s\n", i+1, cardName);
	 }

	// Build buyPriority based on 15 cards
	 /*
	count = 0;
	for(i = 0; i < 24; i++){ // iterate through all possible buy cards
		for(j = 0; j < 15; j++){ // iterate through the fifteen cards
			if(allBuyPossibilities[i] == fifteen_cards[j]){
				buyPriority[count] = allBuyPossibilities[i];
				//cardNumToName(buyPriority[count], cardName);
				//printf("Count: %d; BuyPossible: %d; FifteenCard: %d; Added %s\n", count, i, j, cardName);
				count++;
			}
		}
	}
	
	if(buyPriority[14] == curse){
		printf("BUG FOUND: curse card appeared\n");
	}*/
	/*
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	 Due to bugs, fifteen_cards is automatically buy priority via randomness
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	*/
	fprintf(file, "\nBuy Card Priority this game is as follows: \n");
	for(i = 0; i < 15; i++){
		buyPriority[i] = fifteen_cards[i];
		cardNumToName(buyPriority[i], cardName);
		fprintf(file, "%d: %s\n", i+1, cardName);
	}
}